import 'package:cached_network_image/cached_network_image.dart';
//import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wanchong/api/user.dart';
import 'package:wanchong/app/perinfor/menews.dart';
import 'package:wanchong/app/perinfor/petinfo.dart';
import 'package:wanchong/bean/user_info_entity.dart';
import 'package:wanchong/conf.dart';
import 'dart:ui';

import 'package:wanchong/path.dart';
import 'package:wanchong/utils/event_bus.dart';
import 'package:wanchong/utils/http.dart'; 

class Me extends StatefulWidget {
  @override
  _MeState createState() => _MeState();
}

class _MeState extends State<Me> with AutomaticKeepAliveClientMixin {
  int _count = 5;
  UserInfoEntity _userInfo;
  @override
  void initState() {
    eventBus.on(UpdateUser, (arg) {
      print("eventBus 收到了消息");
      ///事件监听
      _getUserBack();
    });
    _getUser();
    super.initState();
  }

  _getUser() {
    ///获取用户数据
    userInfo().listen((UserInfoEntity data) {
      print(data);

      setState(() { 
        _userInfo = data;
      });
    }).onError((e) {
      MyError err = e;
    });
  }
  _getUserBack() {
    ///获取用户数据
    userInfo().listen((UserInfoEntity data) {
      // print(data);
      _userInfo = data;
    }).onError((e) {
      MyError err = e;
    });
  }
  Widget _buildItem(icon, title) {
    return GestureDetector(
      onTap: () {},
      child: Column(
        children: <Widget>[
          Container(width: 30.0, height: 24.0, child: Image.asset(icon)),
          Container(
            height: 10.0,
          ),
          Text(
            title,
            style: TextStyle(fontSize: 11.0, color: Color(0xFF647280)),
            textAlign: TextAlign.center,
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    double h = MediaQueryData.fromWindow(window).padding.top;
    return Scaffold(
      appBar: PreferredSize(
          child: Container(color: Colors.red),
          preferredSize: Size.fromHeight(-h)
          // preferredSize: Size(Screen.width, 0),
          ),
      body: Container(
        child: ListView(
          physics: BouncingScrollPhysics(),
          children: <Widget>[
            _header(),
            _service(),
            _pet(),
            _menu("assets/user/my_icon_shebei.png", "我的设备", () {}),
            _menu("assets/user/my_icon_shiping.png", "我的同城", () {}),
            _menu("assets/user/my_icon_shiping.png", "我的发布", () {
              Navigator.of(context).pushNamed(ReleasePath);
            }),
            _menu("assets/user/my_icon_shoucang.png", "我的收藏", () {
              //进入收藏页面
              Navigator.of(context).pushNamed(mecollectPath);
            }),
            _menu("assets/user/my_icon_shezhi.png", "设置", () {
              //进入设置页面
              Navigator.of(context).pushNamed(SettingHomePath);
            }),
          ],
        ),
      ),
    );
  }

  Widget _pet() {
    return Container(
      color: Color(0xFFFFFFFF),
      height: 285.0,
      margin: EdgeInsets.only(left: 15.0, right: 15.0, top: 12.0),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(5.0),
        child: Container(
          padding: EdgeInsets.all(15.0),
          child: Column(
            children: <Widget>[
              Container(
                width: double.infinity,
                child: Text(
                  "我的宠物",
                  style: TextStyle(
                      color: Color(0xFF3C4450),
                      fontWeight: FontWeight.w700,
                      fontSize: 14.0),
                  textAlign: TextAlign.left,
                ),
              ),
              _userInfo != null && _userInfo.data.myAnimals.length != 0
                  ? Container(
                      color: Color(0xFFFFFFFFF),
                      height: 180.0,
                      margin: EdgeInsets.only(top: 15.0),
                      // child: ListView(
                      //   children: <Widget>[_listPet()],
                      // ),
                      child: _listPet(),
                    )
                  : Container(
                      color: Color(0xFFFAFAFA),
                      height: 180.0,
                      margin: EdgeInsets.only(top: 15.0),
                    ),
              GestureDetector(
                onTap: () {
                  _addPet();
                },
                child: Container(
                  margin: EdgeInsets.only(top: 15.0),
                  // color: Colors.red,
                  width: double.infinity,
                  alignment: Alignment.center,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        child:
                            Image.asset("assets/user/my_pet_icon_add_pet.png"),
                        width: 20.0,
                        height: 20.0,
                      ),
                      Text(
                        "添加宠物",
                        style:
                            TextStyle(fontSize: 12.0, color: Color(0xFFB4BEC6)),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _menu(icon, title, callback) {
    return GestureDetector(
      onTap: () {
        callback();
      },
      child: Container(
        color: Color(0xFFFFFFFF),
        margin: EdgeInsets.only(top: 12.0, left: 15.0, right: 15.0),
        padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
        child: Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 15.0, right: 15.0),
              child: Image.asset(icon),
            ),
            Text(
              title,
              style: TextStyle(
                  fontSize: 14.0,
                  fontWeight: FontWeight.w700,
                  color: Color(0xFF3C4450)),
            ),
            Expanded(
              child: Container(),
            ),
            Container(
              child: Icon(
                Icons.chevron_right,
                color: Color(0xFFB4BEC6),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _header() {
    final _w = MediaQuery.of(context).size.width;
    return Container(
      height: 177.0,
      child: Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          Container(
            height: 177.0,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("assets/user/my_bei.png"),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Center(
              child: Row(
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  //print("点击了");
                  _user();
                },
                child: Container(
                  margin: EdgeInsets.only(left: 15.0, right: 10.0),
                  height: 60.0,
                  width: 60.0,
                  child: ClipOval(
                    // borderRadius: BorderRadius.circular(6.0),
                    child: _userInfo == null
                        ? Image.asset(
                            "assets/user/header.jpeg",
                            fit: BoxFit.cover,
                          )
                        : CachedNetworkImage(
                            imageUrl: _userInfo.data.user.head,
                            fit: BoxFit.cover,
                          ),
                  ),
                ),
              ),
              Expanded(
                child: Text(
                  _userInfo == null ? "" : _userInfo.data.user.nick,
                  style: TextStyle(
                      fontSize: 18.0,
                      color: Color(0xFF000000),
                      fontWeight: FontWeight.w700),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pushNamed(ImHomePath);
                },
                child: Container(
                  margin: EdgeInsets.only(right: 4.0, left: 4.0),
                  width: 25.0,
                  height: 25.0,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image:
                          AssetImage("assets/user/center_order_icon_dpj.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  print("点击了");
                },
                child: Container(
                  margin: EdgeInsets.only(right: 15.0, left: 8),
                  width: 20.0,
                  height: 20.0,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/user/my_icon_kefu.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),
            ],
          )),

          ///首页中间部分
          Positioned(
              bottom: -35.0,
              left: (_w - 345.0) / 2,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(5.0),
                child: Container(
                  height: 70.0,
                  width: 345.0,
                  decoration: new BoxDecoration(
                    //線性漸變
                    gradient: LinearGradient(
                        colors: [Color(0xFF3F3F3F), Color(0xFF222222)],
                        begin: FractionalOffset(1, 0),
                        end: FractionalOffset(0, 1)),
                  ),
                  child: Stack(
                    children: <Widget>[
                      GestureDetector(
                        onTap: () {
                          //print("去签到");
                          _goSignin();
                        },
                        child: Stack(
                          children: <Widget>[
                            Positioned(
                              right: 5.0,
                              child: Container(
                                height: 40.0,
                                width: 90,
                                //padding: EdgeInsets.only(top: 7.5),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  image: DecorationImage(
                                    image: AssetImage(
                                        "assets/user/my_vip_qiandao_button.png"),
                                    fit: BoxFit.cover,
                                  ),
                                ),
                                child: Text(
                                  '签到领积分',
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Color(0xFF000000),
                                    fontSize: 12,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ))
        ],
      ),
    );
  }

  Widget _service() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(5.0),
      child: Container(
        color: Color(0xFFFFFFFF),
        margin: EdgeInsets.only(left: 15.0, top: 40.0, right: 15.0),
        padding: EdgeInsets.all(15.0),
        height: 115.0,
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Text("我的订单",
                    style: TextStyle(
                        fontSize: 14.0,
                        color: Color(0xFF3C4450),
                        fontWeight: FontWeight.w700)),
                Expanded(
                  child: Container(),
                ),
                Text(
                  "全部订单",
                  style: TextStyle(fontSize: 11.0, color: Color(0xFFB4BEC6)),
                ),
                Container(
                  child:
                      Image.asset("assets/user/center_order_icon_fanhui.png"),
                  width: 10.0,
                  height: 10.0,
                )
              ],
            ),
            Container(
              margin: EdgeInsets.only(top: 10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  _buildItem("assets/user/center_order_icon_dfk.png", "待付款"),
                  _buildItem("assets/user/center_order_icon_dfw.png", "待发货"),
                  _buildItem("assets/user/center_order_icon_dsh.png", "待收货"),
                  _buildItem("assets/user/center_order_icon_dpj.png", "待评价"),
                  _buildItem("assets/user/center_order_icon_dfk.png", "退款/售后"),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  _addPet() {
    Navigator.of(context).pushNamed(AddPetPath);
  }

  void _user() {
    // Navigator.of(context).pushNamed(UserInfoPath);
    Navigator.push(context, MaterialPageRoute( builder: (context)=>UserInfo(user: _userInfo.data.user,)));
  }

  void _goSignin() {
    Navigator.of(context).pushNamed(signinPath);
  }

  Widget _listPet() {
    List<Widget> tiles = [];
    Widget Container1;
    for (UserInfoDataMyAnimal item in _userInfo.data.myAnimals) {
      tiles.add(new GestureDetector(
          onTap: () {
            print(item.animalId);
            // Navigator.of(context)
            //     .pushNamed(PetInfoPath, arguments: {"id": item.animalId});
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => PetInfo(id: item.animalId)));
          },
          child: Container(
            color: Colors.white,
            height: 70.0,
            child: Stack(
              children: <Widget>[
                Positioned(
                    width: 50.0,
                    height: 50.0,
                    left: 0,
                    top: 10.0,
                    child: Container(
                      width: 50.0,
                      height: 50.0,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(50.0),
                      ),
                      child: CircleAvatar(
                        backgroundImage: CachedNetworkImageProvider(
                          item.header,
                        ),
                        radius: 50.0,
                      ),
                    )),
                Positioned(
                  left: 55,
                  top: 15.0,
                  width: 200,
                  child: Column(
                    children: <Widget>[
                      Container(
                        alignment: Alignment.centerLeft,
                        height: 20,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              width: 80.0,
                              child: Text(
                                item.nick,
                                style: TextStyle(
                                    fontWeight: FontWeight.w700,
                                    color: Color(0xFF647280),
                                    fontSize: 12),
                              ),
                            ),
                            Container(
                              width: 16,
                              height: 16,
                              child: Image.asset(
                                item.sex == 1
                                    ? "assets/pet/my_pet_icon_sex_b.png"
                                    : "assets/pet/my_pet_icon_sex_g.png",
                                fit: BoxFit.cover,
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        height: 20,
                        child: Row(
                          children: <Widget>[
                            Text(
                              item.categoryName,
                              style: TextStyle(
                                  color: Color(0xFFB4BEC6), fontSize: 12),
                            ),
                            Text(
                              ' . ',
                              style: TextStyle(
                                  color: Color(0xFFB4BEC6), fontSize: 12),
                            ),
                            Text(
                              item.birthday,
                              style: TextStyle(
                                  color: Color(0xFFB4BEC6), fontSize: 12),
                            ),
                            Text(
                              ' . ',
                              style: TextStyle(
                                  color: Color(0xFFB4BEC6), fontSize: 12),
                            ),
                            Text(
                              item.weight.toString(),
                              style: TextStyle(
                                  color: Color(0xFFB4BEC6), fontSize: 12),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                item.bindStatus
                    ? Positioned(
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(50),
                          child: Container(
                            color: Color(0xFF4DC974),
                            padding: EdgeInsets.only(
                                left: 4.0, right: 4.0, top: 2.0, bottom: 2.0),
                            child: Text(
                              item.bindMsg,
                              style: TextStyle(
                                  color: Color(0xFFFFFFFF), fontSize: 10),
                            ),
                          ),
                        ),
                        //设置定位，
                        right: 10,
                        top: 10.0,
                      )
                    : Positioned(
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(50),
                          child: Container(
                            color: Color(0xFFEEEEEE),
                            padding: EdgeInsets.only(
                                left: 4.0, right: 4.0, top: 2.0, bottom: 2.0),
                            child: Text(
                              item.bindMsg,
                              style: TextStyle(
                                  color: Color(0xFFFFFFFF), fontSize: 10),
                            ),
                          ),
                        ),

                        //设置定位，
                        right: 10,
                        top: 10.0,
                      ),
              ],
            ),
          )));
    }
    Container1 = new ListView(
      children: tiles,
      physics: BouncingScrollPhysics(),
    );
    return Container1;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
